package com.wolfsea.launcherdemo
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewGroup
import androidx.core.view.get
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 *@desc BottomContentLayout
 *@author liuliheng
 *@time 2022/12/21  1:04
 **/
class BottomContentLayout @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : ViewGroup(context, attributeSet) {

    private val mRecyclerView: RecyclerView

    init {
        inflate(context, R.layout.bottom_content_layout, this)
        mRecyclerView = findViewById(R.id.bottom_content_rv)
        Log.d(TAG, "init")
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        Log.d(TAG, "onFinishInflate")

        val dataSet = mutableListOf<BottomLayoutItem>()
        val dataArr = arrayOf(
            "分类领域提出了新型度量学习方法",
            "广泛应用于各种搜索、识别等领域中",
            "度量学习是一种机器学习方法",
            "度量学习可以用来对文本进行分类",
            "随着深度学习的兴起",
            "根据这些特征来识别说话人的语音内容",
            "论文对图像分类以及识别领域大为盛行",
            "类中心W与样本特征",
            "示意图如下",
            "基于上述的优缺点分析",
            "加权平均的方式",
            "具体过程与效果如下图所示",
            "人工智能再次走进大众视野",
            "揭秘AI绘画背后的模型算法",
            "人工智能能实现的功能非常有限",
            "已经成为各行各业智囊团",
            "能给予一些心理建议与安慰",
            "Google搜索引擎的未来带来巨大挑战",
            "一些员工的任务是去构建生成艺术",
            "ChatGPT采用了从人类反馈中强化学习",
            "使用监督微调训练了一个初始模型",
            "OpenAI也需要收集比较数据",
        )

        for (item in dataArr) {
            dataSet.add(BottomLayoutItem(item))
        }

        mRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = BottomContentRvAdapter(dataSet)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val view = get(0)
        measureChildWithMargins(view, widthMeasureSpec, 0, heightMeasureSpec, 0)
        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec) - paddingStart - paddingEnd,
            MeasureSpec.getSize(heightMeasureSpec) - paddingTop - paddingBottom
        )
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        val child = get(0)
        val childWidth = child.measuredWidth
        val childHeight = child.measuredHeight
        val layoutParams = child.layoutParams as MarginLayoutParams
        child.layout(
            layoutParams.leftMargin,
            layoutParams.topMargin,
            layoutParams.topMargin + childWidth,
            layoutParams.topMargin + childHeight
        )
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        performClick()
        return get(0).onTouchEvent(event)
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams =
        MarginLayoutParams(context, attrs)

    fun childResumeOriginState(): Boolean =
        ((get(0) as RecyclerView).layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition() == 0


    companion object {
        const val TAG = "BottomContentLayout"
    }

}